package com.mysiteforme.admin.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityBanner;
import com.mysiteforme.admin.service.ElectricityBannerService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * <p>
 * banner表  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-18
 */
@Controller
@RequestMapping("/admin/electricityBanner")
public class ElectricityBannerController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityBannerController.class);

    @Autowired
    private ElectricityBannerService electricityBannerService;

    @GetMapping("list")
    @SysLog("跳转banner表列表")
    public String list(){
        return "/admin/electricityBanner/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求banner表列表数据")
    public LayerData<ElectricityBanner> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityBanner> layerData = new LayerData<>();
        EntityWrapper<ElectricityBanner> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String bannerUrl = (String) map.get("bannerUrl");
            if(StringUtils.isNotBlank(bannerUrl)) {
                wrapper.like("banner_url",bannerUrl);
            }else{
                map.remove("bannerUrl");
            }

            String link = (String) map.get("link");
            if(StringUtils.isNotBlank(link)) {
                wrapper.like("link",link);
            }else{
                map.remove("link");
            }

            String status = (String) map.get("status");
            if(StringUtils.isNotBlank(status)) {
                wrapper.like("status",status);
            }else{
                map.remove("status");
            }

            String bannerType = (String) map.get("bannerType");
            if(StringUtils.isNotBlank(bannerType)) {
                wrapper.eq("banner_type",bannerType);
            }else{
                map.remove("bannerType");
            }

            String bannerSort = (String) map.get("bannerSort");
            if(StringUtils.isNotBlank(bannerSort)) {
                wrapper.like("banner_sort",bannerSort);
            }else{
                map.remove("bannerSort");
            }

        }
        Page<ElectricityBanner> pageData = electricityBannerService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增banner表页面")
    public String add(){
        return "/admin/electricityBanner/add";
    }

    @PostMapping("add")
    @SysLog("保存新增banner表数据")
    @ResponseBody
    public RestResponse add(ElectricityBanner electricityBanner){
        electricityBannerService.insert(electricityBanner);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑banner表页面")
    public String edit(Long id,Model model){
        ElectricityBanner electricityBanner = electricityBannerService.selectById(id);
        model.addAttribute("electricityBanner",electricityBanner);
        return "/admin/electricityBanner/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑banner表数据")
    public RestResponse edit(ElectricityBanner electricityBanner){
        if(null == electricityBanner.getId() || 0 == electricityBanner.getId()){
            return RestResponse.failure("ID不能为空");
        }
        electricityBannerService.updateById(electricityBanner);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除banner表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityBanner electricityBanner = electricityBannerService.selectById(id);
        electricityBanner.setDelFlag(true);
        electricityBannerService.updateById(electricityBanner);
        return RestResponse.success();
    }

}